package com.google.hackathon.game.shriko.core;

import static playn.core.PlayN.*;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.Layer;

public class Ghost {
  private ImageLayer layer;
  private Body body;
  private float radius;

  public Ghost(World world, Image image, float radius, float x, float y) {
    this.radius = radius;
    layer = graphics().createImageLayer(image);
    layer.setOrigin(image.width() / 2f, image.height() / 2f);
    layer.setScale((radius * 2) / image.width(), (radius * 2) / image.height());
    layer.setTranslation(x, y);

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DYNAMIC;
    bodyDef.position = new Vec2(x, y);
    body = world.createBody(bodyDef);
    FixtureDef fixtureDef = new FixtureDef();
    // TODO(bonbori): fix
    fixtureDef.shape = new CircleShape();
    fixtureDef.shape.m_radius = radius;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.3f;
    body.createFixture(fixtureDef);
  }

  public void paint(float delta) {
    layer.setTranslation(body.getPosition().x, body.getPosition().y);
  }

  public float getRadius() {
    return radius;
  }

  public Layer getLayer() {
    return layer;
  }

  public void setInvisible() {
    layer.setVisible(false);
  }

  public float getX() {
    return body.getPosition().x;
  }

  public float getY() {
    return body.getPosition().y;
  }
}
